package dfs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 子集2 {
    List<List<Integer>> lists = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
       if (nums==null)
           return lists;
       //排序
        Arrays.sort(nums);
       List<Integer> list = new ArrayList<>();
       dfs(nums,list,0);
       return lists;
    }
    public void dfs(int[] nums, List<Integer> list, int n) {
        lists.add(new ArrayList<>(list));
        for (int i = n; i < nums.length; i++) {
            //跳过重复数字
            if (i>n && nums[i]==nums[i-1])
                continue;
            list.add(nums[i]);
            dfs(nums,list,i+1);
            list.remove(list.size()-1);
        }
    }
}
